import os
import uuid
from fastapi import UploadFile
from pathlib import Path

def ensure_data_dirs():
    """确保数据目录存在"""
    Path("data/input").mkdir(parents=True, exist_ok=True)
    Path("data/output").mkdir(parents=True, exist_ok=True)

def save_uploaded_file(upload_file: UploadFile) -> str:
    """保存上传的文件"""
    ensure_data_dirs()

    # 生成唯一文件名
    file_extension = Path(upload_file.filename).suffix
    unique_filename = f"{uuid.uuid4()}{file_extension}"
    file_path = f"data/input/{unique_filename}"

    # 保存文件
    with open(file_path, "wb") as buffer:
        content = upload_file.file.read()
        buffer.write(content)

    return file_path